<script>
    function playMusic(audioUrl) {
        var player = document.getElementById('audioPlayer');
        // 显示播放器
        player.style.display = 'block';

        player.src = audioUrl;
        player.play();
    }

    $('#carouselExample').on('slide.bs.carousel', function (e) {
        var $e = $(e.relatedTarget);
        var idx = $e.index();
        var itemsPerSlide = 3;
        var totalItems = $('.carousel-item').length;

        if (idx >= totalItems - (itemsPerSlide - 1)) {
            var it = itemsPerSlide - (totalItems - idx);
            for (var i = 0; i < it; i++) {
                // append slides to end
                if (e.direction == "left") {
                    $('.carousel-item').eq(i).appendTo('.carousel-inner');
                } else {
                    $('.carousel-item').eq(0).appendTo('.carousel-inner');
                }
            }
        }
    });


</script>
<style>
    #audioPlayer {
        display: none; /* Initially hide the audio player */
        position: fixed;
        bottom: 0;
        left: 0;
        width: calc(100% - 230px);
        margin-left: 240px;
        z-index: 100;
    }

    /* Title Bar Style */
    .title-bar {
        background-color: #333; /* Dark background for the title bar */
        padding: 10px 0; /* Some padding above and below the title */
        margin-bottom: 20px; /* Margin below the title bar */
    }

    .title-bar h2 {
        color: #fff; /* White text color for the title */
        margin: 0; /* Remove default margins from h2 */
        font-size: 1.8em; /* Slightly larger font size for the title */
        font-weight: normal; /* Normal font weight for a more modern look */
        letter-spacing: 2px; /* Add some spacing between letters */
        text-align: center; /* Center the title text */
    }

    /* Carousel Item and Card Style */
    .carousel-item {
        margin-right: 0;
    }

    .carousel-item .img-fluid {
        max-height: 200px;
        width: auto;
        margin: 0 auto;
        transition: opacity 0.3s ease, transform 0.3s ease;
    }

    .card {
        background: #000;
        border: none;
        margin: 10px; /* Add margin for spacing between cards */
        cursor: pointer; /* Change cursor to indicate clickable */
        overflow: hidden; /* Ensure all content fits in the card */
        position: relative; /* Position relative to allow absolute positioning inside */
    }

    .col-md-2 .card {
        height: 250px; /* 示例高度，可根据需求调整 */
        position: relative;
        overflow: hidden;
    }

    .card:hover .img-fluid {
        opacity: 0.5; /* Dim the image a bit to indicate you can interact */
        transform: scale(1.05); /* Scale the image slightly to indicate clickable */
    }

    .card:hover .card-img-overlay {
        background: rgba(0, 0, 0, 0.5); /* Darkens the overlay */
    }

    .card-img-overlay {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        padding: 1.25rem;
        border-radius: calc(0.25rem - 1px); /* Inherit border-radius from card */
        background: rgba(0, 0, 0, 0.6); /* Semi-transparent black background */
        color: white; /* White text color */
        display: flex;
        flex-direction: column;
        justify-content: flex-end; /* Position text at the bottom */
        transition: background-color 0.3s ease;
    }

    .card-img-top {
        width: 100%;
        height: 100%;
        object-fit: cover;
    }

    /* Carousel Controls Style */
    .carousel-control-prev-icon,
    .carousel-control-next-icon {
        background-color: #000;
        border-radius: 50%;
        height: 35px;
        width: 35px;
    }

    .carousel-inner {
        white-space: nowrap;
    }

    .carousel-item .row {
        width: 100%;
        margin-right: 0;
        margin-left: 0;
    }

    .carousel-item .col-md-4 {
        display: inline-block;
        white-space: normal;
    }

    .card-img-top {
        width: 100%; /* 确保图片宽度自适应卡片宽度 */
        height: 160px; /* 设置图片高度，使得卡片看起来更加紧凑 */
        object-fit: cover; /* 确保图片以适当的方式填充指定区域 */
    }

    .title-bar h2 {
        margin-bottom: 20px; /* 为热度推荐标题和卡片之间增加一些间隔 */
    }

    .card-img-overlay {
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        background: rgba(0, 0, 0, 0);
        transition: background 0.5s ease;
    }

    .card:hover .card-img-overlay {
        background: rgba(0, 0, 0, 0.5); /* 鼠标悬停时增加黑色半透明背景 */
    }

    .card-img-overlay h5 {
        transition: transform 0.5s ease; /* 平滑过渡效果 */
    }

    .card:hover .card-img-overlay h5 {
        transform: scale(1.1); /* 鼠标悬停时文字放大 */
    }
</style>
{% extends 'base.html' %}
{% block content %}
    <audio id="audioPlayer" controls></audio>
    <div class="title-bar">
        <h2 class="text-center">歌曲推荐</h2>
    </div>
    <!-- Carousel start -->
    <div id="carouselExample" class="carousel slide" data-ride="carousel" data-interval="false">
        <div class="carousel-inner">
            {% for music in music_list %}
                {% if forloop.counter0|divisibleby:3 %}
                    <div class="carousel-item row no-gutters {% if forloop.first %}active{% endif %}">
                {% endif %}

            <div class="col-md-4">
                <div class="card" onclick="playMusic('{{ music.audio_path }}')">
                    <img class="card-img-top img-fluid" src="{{ music.pic_url }}" alt="{{ music.name }}">
                    <div class="card-img-overlay">
                        <h5 class="card-title">{{ music.name }}</h5>
                        <p class="card-text">{{ music.author }}</p>
                    </div>
                </div>
            </div>

            {% if forloop.counter0|add:"1"|divisibleby:3 or forloop.last %}
                </div>
            {% endif %}
            {% endfor %}
        </div>
        <a class="carousel-control-prev" href="#carouselExample" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
        </a>
        <a class="carousel-control-next" href="#carouselExample" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
    </div>
    <!-- Carousel end -->
    <div class="title-bar">
        <h2 class="text-center">热度推荐</h2>
    </div>
    <div class="container mt-4">
        <div class="row">
            {% for music in heat_music_list %}
                <div class="col-md-2 mb-4">
                    <div class="card h-100" onclick="playMusic('{{ music.audio_path }}')">
                        <img src="{{ music.pic_url }}" class="card-img-top" alt="{{ music.name }}">
                        <!-- 覆盖层显示歌名 -->
                        <div class="card-img-overlay d-flex justify-content-center align-items-center">
                            <h5 class="text-white text-center"
                                style="background-color: rgba(0,0,0,0.5); border-radius: 5px;">{{ music.name }}</h5>
                        </div>
                    </div>
                </div>
            {% endfor %}
        </div>
    </div>
{% endblock %}
